-
-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
consistent use of convert_km and date_trunc through all dashboards, minor fixes #104
base: main
Are you sure you want to change the base?
consistent use of convert_km and date_trunc through all dashboards, minor fixes #104
Conversation
@@ -342,7 +367,7 @@ | |||
"editorMode": "code", | |||
"format": "table", | |||
"rawQuery": true, | |||
"rawSql": "WITH periodic_mileage AS (\r\n SELECT\r\n DATE_TRUNC('$period', start_date) AS period_start,\r\n convert_km(SUM(end_km - start_km)::numeric, '$length_unit') AS total_mileage\r\n FROM\r\n drives\r\n WHERE\r\n car_id = $car_id \r\n GROUP BY\r\n period_start\r\n),\r\ncum_mileage AS (\r\n SELECT\r\n ROW_NUMBER() OVER () AS period_number,\r\n period_start,\r\n total_mileage,\r\n SUM(total_mileage) OVER (ORDER BY period_start) AS cum_mileage\r\n FROM\r\n periodic_mileage\r\n),\r\nBreakEven AS (\r\nSELECT\r\n cm.period_number,\r\n cm.cum_mileage * (0.20 / CASE WHEN '$length_unit' = 'mi' THEN 1 ELSE 1.60934 END) AS depreciation_mileage,\r\n ($depreciated_value - (cm.period_number - 1) * 5.0 / 12) AS depreciation_time\r\nFROM\r\n cum_mileage cm\r\nLEFT JOIN\r\n periodic_mileage mm ON cm.period_start = mm.period_start\r\nORDER BY\r\n cm.period_number\r\n)\r\nSELECT \r\n period_number,\r\n ($car_cost - depreciation_mileage - (($car_cost - depreciation_mileage) * depreciation_time / 100)) * 100 / $car_cost AS percent_depreciated\r\nFROM BreakEven", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loss_rate was hard coded to 0.20 here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the length_unit correction for miles shouldnt be neccessary here.
@@ -640,7 +665,7 @@ | |||
"editorMode": "code", | |||
"format": "table", | |||
"rawQuery": true, | |||
"rawSql": "WITH periodic_mileage AS (\r\n SELECT\r\n DATE_TRUNC('$period', start_date) AS period_start,\r\n convert_km(SUM(end_km - start_km)::numeric, '$length_unit') AS total_mileage\r\n FROM\r\n drives\r\n WHERE\r\n car_id = $car_id \r\n GROUP BY\r\n period_start\r\n),\r\nperiod_cost AS (\r\n SELECT\r\n DATE_TRUNC('$period', start_date) AS period_start,\r\n SUM(cost) AS total_cost\r\n FROM\r\n charging_processes \r\n WHERE\r\n car_id = $car_id \r\n GROUP BY\r\n DATE_TRUNC('$period', start_date)\r\n),\r\ncum_mileage AS (\r\n SELECT\r\n ROW_NUMBER() OVER () AS period_number,\r\n period_start,\r\n total_mileage,\r\n SUM(total_mileage) OVER (ORDER BY period_start) AS cum_mileage\r\n FROM\r\n periodic_mileage\r\n),\r\ncum_cost AS (\r\n SELECT\r\n ROW_NUMBER() OVER () AS period_number,\r\n period_start,\r\n total_cost,\r\n SUM(total_cost) OVER (ORDER BY period_start) AS cum_el_cost\r\n FROM\r\n period_cost\r\n),\r\ncar_current_value AS (\r\n SELECT\r\n $car_cost AS car_current_value\r\n),\r\nBreakEven AS (\r\nSELECT\r\n cm.period_number,\r\n mm.total_mileage AS periodic_mileage,\r\n mc.total_cost AS period_el_cost,\r\n mc.total_cost/mm.total_mileage AS period_cost_per_mileage,\r\n cm.cum_mileage,\r\n cc.cum_el_cost,\r\n cm.cum_mileage * ($loss_rate / CASE WHEN '$length_unit' = 'mi' THEN 1 ELSE 1.60934 END) AS depreciation_mileage,\r\n ($depreciated_value - (cm.period_number - 1) * 5.0 / CASE WHEN '$period' = 'year' THEN 1 ELSE 12 END) AS depreciation_time,\r\n cm.cum_mileage * $fuel_price - cc.cum_el_cost AS cum_el_savings, car_current_value\r\nFROM\r\n cum_mileage cm\r\nLEFT JOIN\r\n cum_cost cc ON cm.period_number = cc.period_number\r\nLEFT JOIN\r\n periodic_mileage mm ON cm.period_start = mm.period_start\r\nLEFT JOIN\r\n period_cost mc ON cm.period_start = mc.period_start\r\nCROSS JOIN\r\n car_current_value\r\nORDER BY\r\n cm.period_number\r\n)\r\nSELECT \r\n period_number, periodic_mileage, period_el_cost, period_cost_per_mileage, cum_mileage, cum_el_cost, depreciation_mileage, depreciation_time, cum_el_savings,\r\n car_current_value - depreciation_mileage - ((car_current_value - depreciation_mileage) * depreciation_time / 100) AS depreciated_car_value,\r\n (car_current_value - depreciation_mileage - ((car_current_value - depreciation_mileage) * depreciation_time / 100)) + (cum_mileage * $fuel_price - cum_el_cost) AS car_value_pls_el_savings\r\nFROM BreakEven", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the length_unit correction for miles shouldnt be neccessary here.
@@ -3229,7 +3265,7 @@ | |||
"hide": false, | |||
"metricColumn": "none", | |||
"rawQuery": true, | |||
"rawSql": "WITH data AS (\n SELECT\n drives.*,\n date_trunc('month', TIMEZONE('UTC', start_date)) as local_period\n FROM drives)\nSELECT\n EXTRACT(EPOCH FROM date_trunc('month', local_period))*1000 AS date_from,\n EXTRACT(EPOCH FROM date_trunc('month', local_period + ('1 ' || 'month')::INTERVAL))*1000 AS date_to,\n CASE 'month'\n WHEN 'month' THEN to_char(local_period, 'YYYY Month')\n WHEN 'year' THEN to_char(local_period, 'YYYY')\n WHEN 'week' THEN 'week ' || to_char(local_period, 'WW') || ' starting ' || to_char(local_period, 'YYYY-MM-DD')\n ELSE to_char(local_period, 'YYYY-MM-DD')\n END AS display,\n local_period AS date,\n sum(GREATEST(start_${preferred_range}_range_km - end_${preferred_range}_range_km, 0) * car.efficiency * 1000) / \n convert_km(sum(distance)::numeric, '$length_unit') as efficiency_net_$length_unit\nFROM data\nJOIN cars car ON car.id = car_id\nWHERE\n car_id = $car_id\nGROUP BY date\nORDER BY date", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case statement not needed, only month available.
@@ -3180,7 +3216,7 @@ | |||
"hide": false, | |||
"metricColumn": "none", | |||
"rawQuery": true, | |||
"rawSql": "WITH data AS (\n SELECT\n charging_processes.*,\n date_trunc('month', TIMEZONE('UTC', start_date)) as local_period\n FROM charging_processes)\nSELECT\n EXTRACT(EPOCH FROM date_trunc('month', local_period))*1000 AS date_from,\n EXTRACT(EPOCH FROM date_trunc('month', local_period + ('1 ' || 'month')::INTERVAL))*1000 AS date_to,\n CASE 'month'\n WHEN 'month' THEN to_char(local_period, 'YYYY Month')\n WHEN 'year' THEN to_char(local_period, 'YYYY')\n WHEN 'week' THEN 'week ' || to_char(local_period, 'WW') || ' starting ' || to_char(local_period, 'YYYY-MM-DD')\n ELSE to_char(local_period, 'YYYY-MM-DD')\n END AS display,\n local_period AS date,\n sum(greatest(charge_energy_used,charge_energy_used)) AS sum_consumption_kwh,\n sum(greatest(charge_energy_used,charge_energy_used)) / count(*) AS avg_consumption_kwh,\n sum(cost) AS cost_charges,\n count(*) AS cnt_charges\nFROM data WHERE\n car_id = $car_id AND\n (charge_energy_used IS NULL OR charge_energy_used > 0.1)\nGROUP BY date\nORDER BY date", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
charge_energy_used,charge_energy_used instead of charge_energy_added,charge_energy_used
c6f9c89
to
e6be94f
Compare
this:
added comments within PR to highlight the minor issues / imrpvoements
Dashboards have been exported using Grafana 11.2.3 (will be available shortly via teslamate-org/teslamate#4338)
Once all PR have been merged I'll export all Dashboards again with Grafana 11.2.3.